package com.efounder.frame.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.drawerlayout.widget.DrawerLayout;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;

import com.core.xml.StubObject;
import com.efounder.frame.activity.helper.EFAppAccountActivityHelper;
import com.efounder.frame.baseui.EFActivity;
import com.efounder.frame.title.EFSlidingTabStripTitleView;
import com.efounder.frame.utils.EFConstants;
import com.efounder.frame.widget.EFSlidingTabStrip;
import com.efounder.frame.xmlparse.EFAppAccountRegistry;
import com.efounder.frame.xmlparse.EFXmlConstants;
import com.efounder.mobilemanager.R;

import java.util.List;

/**
 * Created by hudq on 2016/8/29.
 * 应用号Activity 弃用
 */
@Deprecated
public class EFAppAccountActivity extends EFActivity {
    private EFAppAccountActivityHelper helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ef_activity_app_accounts);
        //初始化xml中数据
        initXmlData();
        //初始化titleView
        EFSlidingTabStrip slidingTabStrip = initTitleView(savedInstanceState);
        helper = new EFAppAccountActivityHelper(this,slidingTabStrip);
        if (savedInstanceState != null){//恢复currentPosition
            int currentPosition = savedInstanceState.getInt("currentPosition");
            helper.setCurrentPosition(currentPosition);
        }
        List<StubObject> oneLevelStubList = EFAppAccountRegistry.getStubListByElementName(EFXmlConstants.ELE_MENU_ROOT);
        helper.createSlidingTabStrip(oneLevelStubList,savedInstanceState);

    }

    private void initXmlData() {
        EFAppAccountRegistry.init(EFConstants.APPS_DIR + "/App1/app_frame.xml");
    }

    private EFSlidingTabStrip initTitleView(Bundle savedInstanceState){
        EFSlidingTabStripTitleView titleView = new EFSlidingTabStripTitleView(this);
        replaceEFTitleView(titleView);
        //左
        ImageView leftImageView = titleView.getLeftImageView();
        int leftImageViewPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,7,getResources().getDisplayMetrics());
        leftImageView.setPadding(leftImageViewPadding,leftImageViewPadding,leftImageViewPadding,leftImageViewPadding);
        leftImageView.setImageDrawable(Drawable.createFromPath(EFConstants.APPS_DIR + "/App1/image/icon"));
        //设置允许打开左边抽屉
        initDrawerLayout(savedInstanceState);
        leftImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getDrawerLayout().openDrawer(Gravity.LEFT);
            }
        });
        //右
        final ImageView rightImageView = titleView.getRightImageView();
        rightImageView.setPadding(leftImageViewPadding,leftImageViewPadding,leftImageViewPadding,leftImageViewPadding);
        rightImageView.setImageResource(R.drawable.ef_title_view_more_normal);
        rightImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<StubObject> menuStubList = EFAppAccountRegistry.getStubListByElementName(EFXmlConstants.ELE_POPUP_MENU);
                helper.createPopupMenu(rightImageView,menuStubList);
            }
        });

        //中间
        EFSlidingTabStrip slidingTabStrip = (EFSlidingTabStrip) titleView.getCenterView();

        return  slidingTabStrip;

    }

    /**
     * 初始化DrawerLayout
     */
    private void initDrawerLayout(Bundle savedInstanceState){
        //设置允许打开左边抽屉
        getDrawerLayout().setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, Gravity.LEFT);
        if (savedInstanceState == null){
            //TODO 设置左边抽屉Fragment
            try {
                FragmentManager fragmentManager = getSupportFragmentManager();
                Fragment leftFragment = (Fragment) Class.forName("com.efounder.fragment.AppAccountLeftDrawerFragment").newInstance();
                fragmentManager.beginTransaction().replace(getDrawerLayoutLeftLayoutResId(),leftFragment).commit();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("currentPosition",helper.getCurrentPosition());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EFAppAccountRegistry.clearRegister();
    }
}
